/*
* $Id: ChangeHousingWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import apiary.Apiary;
import apiary.db.HousingStore.HousingRecord;
import apiary.db.managers.ConvertData;
import apiary.db.managers.DBManager;
import apiary.common.FormHelper;
import apiary.ui.BackScreen;
import apiary.ui.journal.*;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.HousingForm.HousingValue;

/**
 * Wizard of changing hive
 * @author lexus
 */
public class ChangeHousingWizard extends Wizard
{
    private ChooseForm familyChoose;

    /**
     * Constructror
     */
    public ChangeHousingWizard() {
        super(null);
        Init();
    }

    /**
     * Constructror
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ChangeHousingWizard(BackScreen parentForm) {
        super(null);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Saves result of wizard
     */
    public void save() {
        ChooseValue value = (ChooseValue) getData(new Integer(3));
        HousingValue housing = (HousingValue) getData(new Integer(4));
        DBManager.getInstance().changeHousing(value.id, housing);
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen form = (WizardScreen) nextForm;
        if(form.getIdForm().intValue() == 3){
            ChooseValue value = (ChooseValue) getData(new Integer(1));
            int idFamily = 0;
            if(value.id == 1) {
                value = (ChooseValue) getData(new Integer(2));
                idFamily = value.id;
            }
            FormHelper.fillHousingList((ChooseForm) form, idFamily);
        } else if(form.getIdForm().intValue() == 4) {
            ChooseValue value = (ChooseValue) getData(new Integer(3));
            setHousingData((HousingForm) form, value.id);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        if(idForm.intValue() == 1) {
            ChooseValue value = (ChooseValue) dataForm;
            if(value.id == 1) {
                Apiary apiary = Apiary.getInstance();
                apiary.Display(familyChoose);
                setData(idForm, dataForm);
                return false;
            }
        }
        return true;
    }

    /**
     * Form is filled of data of hive
     * @param form form
     * @param id identificator of hive
     */
    private void setHousingData(HousingForm form, int id) {
        DBManager dBManager = DBManager.getInstance();
        HousingRecord housing = dBManager.getHousing(id);
        HousingValue value = form.new HousingValue();
        ConvertData.HousingRecordToValue(housing, value);
        form.setHousingValue(value);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChooseHousing"),
                                   idChoose, this, true);
        FormHelper.fillChooseQBHList((ChooseForm) firstForm,
                                     apiary.getLocaleString("FreeHousing"),
                                     apiary.getLocaleString("ChooseFamily"));

        Integer idChooseF = new Integer(2);
        familyChoose = new ChooseForm(apiary.getLocaleString("ChooseFamily"),
                                      idChooseF, this, true);
        familyChoose.setBackScreen(firstForm);
        FormHelper.fillFamilyList(familyChoose);

        Integer idChooseH = new Integer(3);
        ChooseForm chooseForm = new ChooseForm(
                apiary.getLocaleString("ChooseHousing"), idChooseH, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChooseF, chooseForm);
        formsNext.put(idChoose, chooseForm);

        Integer idHousing = new Integer(4);
        HousingForm housingForm = new HousingForm(
                apiary.getLocaleString("ChangeHousing"), idHousing, this, false);
        housingForm.setBackScreen(chooseForm);
        formsNext.put(idChooseH, housingForm);
    }
}
